package chapter07

import scala.collection.mutable

object Demo16_MergeMap {

  def main(args: Array[String]): Unit = {
    val map1 = Map("a" -> 1 , "b" -> 2 , "c" -> 8)
    val map2 = mutable.Map("a" -> 4 , "b" -> 3 , "c" -> 9 , "d" -> 7)

    val map3 = map1.foldLeft(map2)(
      (mergedMap , kv) => {
        val key = kv._1
        val value = kv._2
        mergedMap(key) = mergedMap.getOrElse(key,0) + value
        mergedMap
      }
    )
    println(map3)
  }

}
